package com.jack.xiaoer.util;

/**
 * Created by Jackaroo Zhang on 2018/10/11.
 */


/**
 * Web层返回的响应对象，用来承载请求的结果状态和数据
 */
public class AjaxResponse {

    // 响应状态对象
    private State state;
    // 响应的数据，T为泛型
    private Object result;
    // Token令牌
    private String token;

    /**
     * 响应对象一般通过静态方法构造，所以将构造方法私有化
     * @param state
     * @param result
     */
    private AjaxResponse(State state, Object result) {
        this(state, result, null);
    }

    private AjaxResponse(State state) {
        this(state, null, null);
    }

    /**
     * 构造一个成功状态的响应
     * @param msg 响应状态的描述信息
     * @param result 响应的数据
     * @param token Token令牌
     * @return
     */
    public static AjaxResponse success(String msg, Object result, String token) {
        return new AjaxResponseBuilder().state(StateCodeEnum.SUCCESS, msg).result(result).token(token).build();
    }

    public static AjaxResponse success(String msg, Object result) {
        return success(msg, result, null);
    }

    public static AjaxResponse success(String msg) {
        return success(msg, null, null);
    }

    /**
     * 构造一个失败状态的响应，请求了一个不存在的资源
     * @param msg
     * @param result
     * @return
     */
    public static AjaxResponse fail(String msg, Object result) {
        return new AjaxResponseBuilder().state(StateCodeEnum.FAIL, msg).result(result).build();
    }

    public static AjaxResponse fail(String msg) {
        return fail(msg, null);
    }

    /**
     * 构造一个错误状态的响应，服务器内部运行错误
     * @param msg
     * @param result
     * @return
     */
    public static AjaxResponse error(String msg, Object result) {
        return new AjaxResponseBuilder().state(StateCodeEnum.ERROR, msg).result(result).build();
    }

    public static AjaxResponse error(String msg) {
        return error(msg, null);
    }

    public static AjaxResponse error() {
        return error("服务器异常", null);
    }

    /**
     * 构造一个无效参数状态的响应，传递的参数错误
     * @param msg
     * @param result
     * @return
     */
    public static AjaxResponse invalid(String msg, Object result) {
        return new AjaxResponseBuilder().state(StateCodeEnum.INVALID_PARAM, msg).result(result).build();
    }

    public static AjaxResponse invalid(String msg) {
        return invalid(msg, null);
    }


    private AjaxResponse(AjaxResponseBuilder builder) {
        this.state = builder.state;
        this.result = builder.result;
        this.token = builder.token;
    }

    private AjaxResponse(State state, Object result, String token) {
        this.state = state;
        this.result = result;
        this.token = token;
    }


    public static class AjaxResponseBuilder {
        // 响应状态对象
        private State state;
        // 响应的数据，T为泛型
        private Object result;
        // Token令牌
        private String token;

        public AjaxResponseBuilder state(StateCodeEnum stateCode, String msg) {
            this.state = new State(stateCode, msg);
            return this;
        }

        public AjaxResponseBuilder result(Object result) {
            this.result = result;
            return this;
        }

        public AjaxResponseBuilder token(String token) {
            this.token = token;
            return this;
        }

        public AjaxResponse build() {
            return new AjaxResponse(this);
        }
    }

    public enum StateCodeEnum {
        // 响应的状态码，根据需求后面会继续添加几种状态
        SUCCESS(2000), FAIL(4000), INVALID_PARAM(4001), ERROR(5000);
        private Integer code;
        StateCodeEnum(Integer code) {
            this.code = code;
        }
    }

    static class State {
        // 响应状态的状态码
        private Integer code;
        // 响应状态的描述信息
        private String msg;

        public State(StateCodeEnum code, String msg) {
            this.code = code.code;
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }


/*----------------------------------------------Getter And Setter-----------------------------------------------------*/

    public State getState() {
        return state;
    }

    public void setState(State tmState) {
        this.state = tmState;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public String getToken() {
        return token;
    }
}
